package com.stylefeng.guns.modular.netcar.controller;

import cn.hutool.core.bean.BeanUtil;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.Tools.CodeMsg;
import com.stylefeng.guns.core.common.Tools.Result;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.core.validater.Insert;
import com.stylefeng.guns.core.validater.Update;
import com.stylefeng.guns.modular.netcar.model.VehicleInf;
import com.stylefeng.guns.modular.netcar.service.IVehicleInfService;
import com.stylefeng.guns.modular.netcar.warpper.VehicleInfWarpper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 车辆信息
 *
 * @author sasazaixian @Date 2018-10-23 11:06
 */
@Controller
@RequestMapping(value = {"/gunsApi/auth/vehicleInf"})
@Api(
    value = "车辆信息操作相关api",
    tags = {"车辆信息操作接口"})
@Slf4j(topic = "车辆信息")
public class VehicleInfController extends BaseController {
  @Autowired private IVehicleInfService vehicleInfService;

  /**
   * 分页获取车辆信息列表
   *
   * @param vehicleInf
   * @param pageNum
   * @param pageSize
   * @return
   */
  @ApiOperation(value = "分页获取车辆信息列表", notes = "分页获取车辆信息列表", httpMethod = "POST")
  @PostMapping(
      value = "/pageList",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  @ResponseBody
  public Result pageList(
      @RequestBody @ApiParam(name = "vehicleInf", value = "车辆信息对象") VehicleInf vehicleInf,
      @ApiParam(name = "pageNum", value = "当前页数", required = true) @RequestParam(value = "pageNum", defaultValue = "1",required = true)Integer pageNum,
      @ApiParam(name = "pageSize", value = "每页显示条数", required = true)@RequestParam(value = "pageSize", defaultValue = "1000000",required = true) Integer pageSize) {
    try {
      Pager pager = new Pager();
      pager.setPageNumber(pageNum); // 页码从1 开始
      pager.setPageSize(pageSize);
      pager.setObj(vehicleInf);
      Map map = this.vehicleInfService.pageListVehicleInf(pager, getToken());
      if (map.size() > 0) {
        List<VehicleInf> list = (List<VehicleInf>) map.get("rows");
          List<Map<String, Object>> mList=new ArrayList<>();
          for (VehicleInf o:list){
              Map map1= BeanUtil.beanToMap(o);
              mList.add(map1);
          }
          super.warpObject(new VehicleInfWarpper(mList));
          BeanUtil.setFieldValue(map,"rows",mList);
      }
      
      return Result.success(map);
    } catch (Exception e) {
      return Result.error(CodeMsg.SERVER_EXCEPTION, e.getMessage());
    }
  }
  /**
   * 获取车辆信息列表
   *
   * @param vehicleInf
   * @return
   */
  @ApiOperation(value = "获取车辆信息列表", notes = "获取车辆信息列表", httpMethod = "POST")
  @PostMapping(
      value = "/list",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  @ResponseBody
  public Result list(
      @RequestBody @ApiParam(name = "vehicleInf", value = "车辆信息对象") VehicleInf vehicleInf) {
    try {
      return Result.success(vehicleInfService.selectListVehicleInf(vehicleInf, getToken()));
    } catch (Exception e) {
      return Result.error(CodeMsg.SERVER_EXCEPTION, e.getMessage());
    }
  }
  /**
   * 通过主键获取车辆信息对象
   *
   * @param id
   * @return
   */
  @ApiOperation(value = "通过主键获取车辆信息对象", notes = "通过主键获取车辆信息对象", httpMethod = "GET")
  @GetMapping(value = "/getById", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  @ResponseBody
  public Result getById(@ApiParam(name = "id", value = "主键ID", required = true) @RequestParam(value = "id", required = true)  String id) {
    try {
      List<Map<String, Object>> mList=new ArrayList<>();
      VehicleInf vehicleInf=vehicleInfService.findVehicleInfById(id, getToken());
      if (vehicleInf != null) {
        Map map1 = BeanUtil.beanToMap(vehicleInf);
        mList.add(map1);
        }
      return Result.success(super.warpObject(new VehicleInfWarpper(mList)));
    } catch (Exception e) {
      return Result.error(CodeMsg.SERVER_EXCEPTION, e.getMessage());
    }
  }

  /**
   * 新增车辆信息数据
   *
   * @param vehicleInf
   * @param result
   * @return
   */
  @ApiOperation(value = "新增车辆信息数据", notes = "新增车辆信息数据", httpMethod = "POST")
  @PostMapping(value = "/add",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  @BussinessLog(value = "新增车辆信息数据")
  @ResponseBody
  public Result add(@Validated(value = Insert.class) @RequestBody @ApiParam(name = "vehicleInf ", value = "车辆信息对象") VehicleInf vehicleInf,BindingResult result) {
    if (result.hasErrors()) {
      return Result.error(CodeMsg.SERVER_EXCEPTION, "参数异常！");
    }
    try {
      vehicleInfService.insertVehicleInf(vehicleInf, getToken());
      return Result.success();
    } catch (Exception e) {
      String msg=e.getMessage();
      if(msg.indexOf("for key 'index_2'")>-1 || msg.indexOf("for key 'index_4'")>-1 ||msg.indexOf("for key 'index_3'")>-1){
        msg="车牌号+车牌颜色/发动机编号/编码已经存在！";
      }
      return Result.error(CodeMsg.SERVER_EXCEPTION,msg);
    }
  }

  /**
   * 更新车辆信息数据
   *
   * @param vehicleInf
   * @param result
   * @return
   */
  @ApiOperation(value = "更新车辆信息数据", notes = "更新车辆信息数据", httpMethod = "POST")
  @PostMapping(
      value = "/update",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  @BussinessLog(value = "更新车辆信息数据")
  @ResponseBody
  public Result update(
      @Validated(value = Update.class) @RequestBody @ApiParam(name = "vehicleInf", value = "车辆信息对象")
          VehicleInf vehicleInf,
      BindingResult result) {
    if (result.hasErrors()) {
      return Result.error(CodeMsg.SERVER_EXCEPTION, "参数异常！");
    }
    try {
      vehicleInfService.updateVehicleInf(vehicleInf, getToken());
      return Result.success();
    } catch (Exception e) {
      String msg=e.getMessage();
      if(msg.indexOf("for key 'index_2'")>-1 || msg.indexOf("for key 'index_4'")>-1 ||msg.indexOf("for key 'index_3'")>-1){
        msg="车牌号+车牌颜色/发动机编号/编码已经存在！";
      }
      return Result.error(CodeMsg.SERVER_EXCEPTION,msg);
    }
  }
  /**
   * 删除车辆信息数据
   *
   * @param ids
   * @return
   */
  @ApiOperation(value = "删除车辆信息数据", notes = "删除车辆信息数据", httpMethod = "GET")
  @GetMapping(value = "/delBatch", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  @BussinessLog(value = "删除车辆信息数据")
  @ResponseBody
  public Result delBatch(
      @ApiParam(name = "ids", value = "主键ID字符数组", required = true)
          @RequestParam(value = "ids", required = true)
          String ids) {
    try {
      vehicleInfService.deleteBatchVehicleInf(ids.split(","), getToken());
      return Result.success();
    } catch (Exception e) {
      return Result.error(CodeMsg.SERVER_EXCEPTION, e.getMessage());
    }
  }
  /**
   * 删除车辆信息数据
   *
   * @param vehicleInf
   * @return
   */
  @ApiOperation(value = "删除车辆信息数据", notes = "删除车辆信息数据", httpMethod = "POST")
  @PostMapping(
      value = "/del",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  @BussinessLog(value = "删除车辆信息数据")
  @ResponseBody
  public Result del(
      @RequestBody @ApiParam(name = "vehicleInf", value = "车辆信息对象") VehicleInf vehicleInf) {
    try {
      vehicleInfService.deleteVehicleInf(vehicleInf, getToken());
      return Result.success();
    } catch (Exception e) {
      return Result.error(CodeMsg.SERVER_EXCEPTION, e.getMessage());
    }
  }
  /**
   * 获取车辆信息的Key-Value列表，用于html的Select数据源
   *
   * @param vehicleInf
   * @return
   */
  @ApiOperation(
      value = "获取车辆信息的Key-Value列表，用于html的Select数据源",
      notes = "获取车辆信息的Key-Value列表，用于html的Select数据源",
      httpMethod = "GET")
  @GetMapping(
      value = "/optionsList",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  @ResponseBody
  public Result optionsList(
      @RequestBody @ApiParam(name = "vehicleInf", value = "车辆信息对象") VehicleInf vehicleInf) {
    try {
      return Result.success(vehicleInfService.optionsVehicleInf(vehicleInf, getToken()));
    } catch (Exception e) {
      return Result.error(CodeMsg.SERVER_EXCEPTION, e.getMessage());
    }
  }

  private String getToken() {
    return getHttpServletRequest().getHeader("Authorization").substring(7);
  }
}
